package controllers

import (
	"github.com/astaxie/beego"
	"server/exception"
	"server/global"
)

/*
Date:2021/11/20
Time:10:44 下午
*/

type BaseController struct {
	beego.Controller
	apiData []byte
}

func (c *BaseController) Prepare() {
	c.apiData = c.Ctx.Input.RequestBody
}

func (c *BaseController) InputData() {

}

func (c *BaseController) SuccReturn(retData interface{}) {
	retJson := make(map[string]interface{})
	retJson["errCode"] = 0
	retJson["data"] = retData
	c.Data["json"] = retJson
	c.ServeJSON()
	c.StopRun()
}

func (c *BaseController) FailReturn(err error) {
	code := exception.SystemError.Code
	msg := exception.SystemError.Message
	if e, ok := exception.Type(err); ok {
		code = e.Code
		msg = e.Message
	}
	retJson := make(map[string]interface{})
	retJson["errMsg"] = msg
	retJson["errCode"] = code
	c.Data["json"] = retJson
	c.ServeJSON()
	c.StopRun()
}

func (c *BaseController) HandError(e error) {
	if e != nil {
		c.FailReturn(global.NewCodeError(50000))
	}
}
